package com.bill99.mw;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

/**
 * @author sheng.zhang
 * 
 */
public class MainTabActivity extends TabActivity implements
		OnCheckedChangeListener {

	private static final String TAG = "MainTabActivity";

	private TabHost mmTabHost;
	private Intent mmHotRecommendIntent;
	private Intent mmOrdersIntent;
	private Intent mmAccountSettingsIntent;
	private Intent mmSystemInfoIntent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab);
		Log.i(TAG, "onCreate");
		initRadios();
		setupIntent();
	}

	/**
	 * 初始化底部按钮
	 */
	private void initRadios() {
		((RadioButton) findViewById(R.id.maintab_hot_recommend))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.maintab_orders))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.maintab_account_settings))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.maintab_system_info))
				.setOnCheckedChangeListener(this);
	}

	/**
	 * 切换模块
	 */
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.maintab_hot_recommend:
				this.mmTabHost.setCurrentTabByTag("mmHotRecommend_tab");
				break;
			case R.id.maintab_orders:
				this.mmTabHost.setCurrentTabByTag("mmOrders_tab");
				break;
			case R.id.maintab_account_settings:
				this.mmTabHost.setCurrentTabByTag("mmAccountSettings_tab");
				break;
			case R.id.maintab_system_info:
				this.mmTabHost.setCurrentTabByTag("mmSystemInfo_tab");
				break;
			}
		}
	}

	private void setupIntent() {
		this.mmHotRecommendIntent = new Intent(this, HotRecommendActivity.class);
		this.mmOrdersIntent = new Intent(this, OrdersActivity.class);
		this.mmAccountSettingsIntent = new Intent(this, AccountListActivity.class);
		this.mmSystemInfoIntent = new Intent(this, SystemInfoActivity.class);

		this.mmTabHost = getTabHost();
		mmTabHost.setup();

		buildTabContent("mmHotRecommend_tab", this.mmHotRecommendIntent);
		buildTabContent("mmOrders_tab",this.mmOrdersIntent);
		buildTabContent("mmAccountSettings_tab", this.mmAccountSettingsIntent);
		buildTabContent("mmSystemInfo_tab", this.mmSystemInfoIntent);
	}

	private void buildTabContent(String tag, Intent content) {
		this.mmTabHost.addTab(mmTabHost.newTabSpec(tag).setIndicator(tag)
				.setContent(content));
	}
}